package org.hackystat.sdsa.analyzer.control.processor;

import java.util.Map;
import java.util.logging.Logger;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.edit.DocumentEditActivity;
import org.hackystat.sdsa.analyzer.model.activity.edit.ProductionEditActivity;
import org.hackystat.sdsa.analyzer.model.activity.edit.UnitTestEditActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Provides the functionality to process the editing activity.
 * 
 * @author hongbing
 */
public class EditActivityProcessor implements Processor {

  /**
   * Process the sensor data to construct edit activity.
   * 
   * @param sensorData Sensor data.
   * @param logger Logger.
   * @return Edit activity.
   */
  public Activity process(SensorData sensorData, Logger logger) {
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);

    Activity activity = null;
    if (propMap.containsKey(ProductionEditActivity.CLASS_NAME)) {
      if (propMap.containsKey(UnitTestEditActivity.CURRENT_TEST_METHODS)) {
        activity = new UnitTestEditActivity(sensorData, logger);
      }
      else {
        activity = new ProductionEditActivity(sensorData, logger);
      }
    }
    else {
      activity = new DocumentEditActivity(sensorData, logger);
    }

    return activity;
  }

  /**
   * Decides whether a sensordata entry is processable.
   * 
   * @param sensorData Sensor data.
   * @return True if the sensor data is an editing activity.
   */
  public boolean processable(SensorData sensorData) {
    String sensorDataType = sensorData.getSensorDataType();
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    
    if (!"DevEvent".equalsIgnoreCase(sensorDataType)) {
      return false;
    }
    
    if (!propMap.containsKey("type") || !"Edit".equalsIgnoreCase(propMap.get("type"))) {
      return false;
    }
    
    if (!propMap.containsKey("subtype") || 
             (!"StateChange".equalsIgnoreCase(propMap.get("subtype")) &&  
              !"Save".equalsIgnoreCase(propMap.get("subtype")))) {
       return false;     
    }
    
    return true;
  }

}
